package com.yantong.pojo.wxtemplate;


import com.yantong.pojo.TemplateParam;
import lombok.Data;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 微信发送模板消息
 * 参考api : https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Template_Message_Interface.html
 * @author
 */
@Data
public class WxSendTemplateNews {

	/** 接收者openid  */
	private String touser;
	/** 模板ID */
	private String templateId;
	/** 模板跳转链接（海外帐号没有跳转能力） */
	private String url;
	/** 模板数据 */
	private Map<String, String> items;



	public String toJSON() {  
	    StringBuffer buffer = new StringBuffer();  
	    buffer.append("{");  
	    buffer.append(String.format("\"touser\":\"%s\"", this.touser)).append(",");  
	    buffer.append(String.format("\"template_id\":\"%s\"", this.templateId)).append(",");  
	    buffer.append(String.format("\"url\":\"%s\"", this.url)).append(",");
	    buffer.append("\"data\":{");  

		Iterator<Map.Entry<String, String>> iterator = items.entrySet().iterator();
		Map.Entry<String, String> next = iterator.next();
		while (iterator.hasNext()){
			buffer.append(String.format("\"%s\": {\"value\":\"%s\"},", next.getKey(), next.getValue()));
			next = iterator.next();
		}
		buffer.append(String.format("\"%s\": {\"value\":\"%s\"}", next.getKey(), next.getValue()));


	    buffer.append("}");  
	    buffer.append("}");  
	    return buffer.toString();  
	    }  

}
